

package com.hazelcast.internal.serialization.impl.defaultserializers;

import com.hazelcast.internal.serialization.impl.SerializationConstants;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;

import java.io.IOException;
import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * The {@link PriorityBlockingQueue} serializer
 */
public class PriorityBlockingQueueStreamSerializer<E> extends AbstractCollectionStreamSerializer<PriorityBlockingQueue<E>> {

    @Override
    public int getTypeId() {
        return SerializationConstants.JAVA_DEFAULT_TYPE_PRIORITY_BLOCKING_QUEUE;
    }

    @Override
    public void write(ObjectDataOutput out, PriorityBlockingQueue<E> collection) throws IOException {
        out.writeObject(collection.comparator());

        super.write(out, collection);
    }

    @Override
    public PriorityBlockingQueue<E> read(ObjectDataInput in) throws IOException {
        Comparator<E> comparator = in.readObject();
        int size = in.readInt();
        PriorityBlockingQueue<E> collection = new PriorityBlockingQueue<>(Math.max(1, size), comparator);
        return deserializeEntries(in, size, collection);
    }
}
